﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mgh.Data.Sql;
using System.Data;

namespace A2Z.DataAccess
{
    public class MatchDA : Mgh.Data.Sql.ClassBase
    {
        protected MatchDA() { }

        private MatchDA(DataConnection cnn)
        {
            this.IConnection = cnn;
        }

        #region get

        protected dataSource.Fb_Vw_MatchDataTable _getFMatchByCTeamID(string cTeamID1, string cTeamID2, string tourID, string groupID)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    dataSource.Fb_Vw_MatchDataTable tb = new dataSource.Fb_Vw_MatchDataTable();
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (!string.IsNullOrEmpty(tourID))
                    {
                        adap.WhereText = tb.TourIDColumn.ColumnName + "=@tour and ";
                        adap.addParameter("tour", SqlDbType.VarChar, tourID, ParameterDirection.Input);
                    }
                    if (!string.IsNullOrEmpty(groupID))
                    {
                        adap.WhereText = tb.GroupIDColumn.ColumnName + "=@group and ";
                        adap.addParameter("group", SqlDbType.VarChar, groupID, ParameterDirection.Input);
                    }
                    if (!string.IsNullOrEmpty(cTeamID1))
                    {
                        adap.WhereText = tb.GroupIDColumn.ColumnName + "=@team1 and ";
                        adap.addParameter("team1", SqlDbType.VarChar, cTeamID1, ParameterDirection.Input);
                    }
                    if (!string.IsNullOrEmpty(cTeamID2))
                    {
                        adap.WhereText = tb.GroupIDColumn.ColumnName + "=@team2 and ";
                        adap.addParameter("team2", SqlDbType.VarChar, cTeamID2, ParameterDirection.Input);
                    }
                    adap.WhereText = adap.WhereText.Trim().TrimEnd('a', 'n', 'd');
                    if (adap.fillData(tb))
                    {
                        return tb;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return null;
        }

        protected dataSource.Fb_Vw_MatchDataTable _getFMatchsByID(List<string> listID)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    dataSource.Fb_Vw_MatchDataTable tb = new dataSource.Fb_Vw_MatchDataTable();
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    adap.WhereText = tb.MatchIDColumn.ColumnName + " IN (";
                    for (int i = 0; i < listID.Count; i++)
                    {
                        adap.WhereText = "@" + i + ",";
                        adap.addParameter(i.ToString(), SqlDbType.VarChar, listID[i], ParameterDirection.Input);
                    }
                    adap.WhereText = adap.WhereText.Trim().TrimEnd(',') + ")";
                    if (adap.fillData(tb))
                    {
                        return tb;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return null;
        }

        protected dataSource.Fb_Vw_MatchDataTable _getFMatchsByTour(DateTime started, DateTime ended, string tourID, string groupID)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    dataSource.Fb_Vw_MatchDataTable tb = new dataSource.Fb_Vw_MatchDataTable();
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (!string.IsNullOrEmpty(tourID))
                    {
                        adap.WhereText = tb.TourIDColumn.ColumnName + "=@tour and ";
                        adap.addParameter("tour", SqlDbType.VarChar, tourID, ParameterDirection.Input);
                    }
                    if (!string.IsNullOrEmpty(groupID))
                    {
                        adap.WhereText = tb.GroupIDColumn.ColumnName + "=@group and ";
                        adap.addParameter("group", SqlDbType.VarChar, groupID, ParameterDirection.Input);
                    }
                    adap.WhereText = tb.StartDateColumn.ColumnName + ">@started and ";
                    adap.addParameter("started", SqlDbType.VarChar, started, ParameterDirection.Input);
                    adap.WhereText = tb.StartDateColumn.ColumnName + "<@ended and ";
                    adap.addParameter("ended", SqlDbType.VarChar, ended, ParameterDirection.Input);
                    adap.WhereText = adap.WhereText.Trim().TrimEnd('a', 'n', 'd');
                    if (adap.fillData(tb))
                    {
                        return tb;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return null;
        }

        #endregion

        #region insert

        protected bool _insertMatchs(dataSource.Fb_Tbl_MatchDataTable tb)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.addData(tb))
                    {
                        return true;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        protected bool _insertTMatchs(dataSource.Fb_Tbl_TeamMatchDataTable tb)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.addData(tb))
                    {
                        return true;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        #endregion

        #region update

        protected bool _updateMatchs(dataSource.Fb_Tbl_MatchDataTable tb)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.upData(tb))
                    {
                        return true;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        protected bool _updateTMatchs(dataSource.Fb_Tbl_TeamMatchDataTable tb)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.upData(tb))
                    {
                        return true;
                    }
                    else addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber());
                }
                else addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber());
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        protected bool _updateMatchWithColumns(dataSource.Fb_Tbl_MatchDataTable tb, params string[] columns)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.upData(tb, columns))
                    {
                        return true;
                    }
                    else { addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber()); }
                }
                else { addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber()); }
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        protected bool _updateTMatchWithColumns(dataSource.Fb_Tbl_TeamMatchDataTable tb, params string[] columns)
        {
            bool isOpen = false;
            try
            {
                if (openConnect(ref isOpen))
                {
                    DataAdapter adap = new DataAdapter(IConnection, tb.TableName);
                    if (adap.upData(tb, columns))
                    {
                        return true;
                    }
                    else { addMessage(adap.getMsgCode(), adap.getMessage(), adap.getNumber()); }
                }
                else { addMessage(IConnection.getMsgCode(), IConnection.getMessage(), IConnection.getNumber()); }
            }
            finally { closeConnect(isOpen); }
            return false;
        }

        #endregion

        #region delete

        #endregion

    }
}
